package edu.lingnan.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @ClassName MyThreadConfig
 * @Description TODO
 * @Author kang
 * @Date 2022/9/17 下午 4:39
 * @Version 1.0
 */
@Configuration
public class MyThreadConfig {
    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolConfigProperties properties) {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                //核心线程池
                properties.getCoreSize(),
                //线程池最大
                properties.getMaxSize(),
                //空闲线程多少秒后关闭
                properties.getKeepAliveTime(),
                //时间单位
                TimeUnit.SECONDS,
                //阻塞队列长度
                new LinkedBlockingDeque<>(100000),
                //线程工厂(默认)
                Executors.defaultThreadFactory(),
                //拒绝策略
                new ThreadPoolExecutor.AbortPolicy()
        );
        //threadPoolExecutor.prestartCoreThread();
        return threadPoolExecutor;
    }
}
